//
//  LoginApi.swift
//  MintBag
//
//  Created by wy on 2024/9/5.
//

import Foundation

struct DeviceModel {
    var phone: String
    var code: String
    // 手机品牌
    var mobilePhoneBrands: String
    // 应用市场编码
    var appMarketCode: String
    // af用户唯一标识id
    var appsFlyerId: String
    // 设备型号
    var deviceModel: String
    // 渠道（af获取) organic、googleadwords_int、Facebook Ads、Twitter、Palm、shalltry_int、restricted
    var channelCode: String
    // app版本
    var version: String
    // ios
    var client: String
    // af/谷歌拿到的数据（json字符串）
    var extensionS: String
    // adjust用户唯一 标识id
    var adjustId: String
}

enum Api {
    // 获取短信验证码
    case sendCode(phone: String)
    // 登录
    case login(device: DeviceModel)
    // 静默登录
    case staticLogin(version: String)
    // 退出登录
    case logout
    // 获取协议
    case getProtocol
    // 上传图片
    case uploadImg
    // 获取系统版本
    case getVersion
    // 用户埋点
    case uploadIosAction([String: Any])
    // 上传权限信息
    case uploadIosAuth(_ deviceInfo: [String: Any]? = nil, location: [String: Any]? = nil, calendarList: [[String: Any]]? = nil)
        
    var path: String {
        switch self {
        case .sendCode:
            return "account/sendVerifyCode"
        case .login:
            return "account/loginByPhoneVerifyCode"
        case .staticLogin:
            return "account/staticLoginv2"
        case .logout:
            return "account/logout"
        case .getProtocol:
            return "system/getProtocolUrlv2"
        case .uploadImg:
            return "system/uploadimg"
        case .getVersion:
            return "system/getNewVersion"
        case .uploadIosAction:
            return "auth/uploadIosAction"
        case .uploadIosAuth:
            return "auth/uploadIosAuth"
        }
    }
    
    var parameters: [String: Any] {
        switch self {
        case .sendCode(let phone):
            return ["phone": phone]
        case .login(let device):
            return ["phone": device.phone, 
                    "code": device.code,
                    "mobilePhoneBrands": device.mobilePhoneBrands,
                    "appMarketCode": device.appMarketCode,
                    "appsFlyerId": device.appsFlyerId,
                    "deviceModel": device.deviceModel,
                    "channelCode": device.channelCode,
                    "version": device.version,
                    "client": device.client,
                    "extension": device.extensionS,
                    "adjustId": device.adjustId]
        case .staticLogin(let version):
            return ["version": version]
        case .uploadIosAction(let p):
            return p
        case .uploadIosAuth(let deviceInfo, let location, let calendarList):
            var p: [String: Any] = [:]
            if let deviceInfo = deviceInfo {
                p["deviceInfo"] = deviceInfo
            }
            if let location = location {
                p["location"] = location
            }
            if let calendarList = calendarList {
                p["calendarList"] = calendarList
            }
            return p
        default:
            return [:]
        }
    }
    
    var service: String {
        return ""
    }
    
    var headers: [String: String] {
        var h: [String: String] = [
            "appName": Const.appName,
            "devName": "ios",
            "appVersion": "1.0.0",
        ]
        if let userId = KeychainStore.shared.string(forKey: .userId) as String?, !userId.isEmpty {
            h["userId"] = userId
        }
        if let sessionId = KeychainStore.shared.string(forKey: .sessionId) as String?, !sessionId.isEmpty {
            h["session"] = sessionId
        }
        return h
    }
    
    var method: String {
        return "POST"
    }
    
    var domain: String {
        return "https://api.ngmintbag.com/"
    }
}
